#!/bin/bash

#generates *.h files with a model function and plots measurements and model function

gnuplot <<EOF

set macro

set xrange [96:800]
set yrange [0:20]

f(x)=a*x**3+b*x**2+c*x+d
fnmeth(z) = "" . sprintf("%e",a) . "*x**3+" . sprintf("%e",b) . "*x**2+" . sprintf("%e",c) . "*x+" . sprintf("%e",d)
FITLIMIT=1e-6

#do for [i=0 method in "naiv daxpy"] {
#	fit f(x) "outfiles/" . method . ".w.plot.txt" using 1:2 via a,b,c,d
#	set print method . ".h"
#	print sprintf("#define %s_model(x) %e *x*x*x+ %e *x*x+ %e *x+ %e", method, a,b,c,d)
#	set print
#}

set xlabel "Matrixgroesse n"
set ylabel "Rechenzeit [sec]"

method = "naiv"
set title "Messung und Approx: " . method
fit f(x) "outfiles/" . method . ".w.plot.txt" using 1:2 via a,b,c,d
str=fnmeth(0)
naiv(x)=@str
set print method . ".h"
print sprintf("#define %s_model(x) %e *x*x*x+ %e *x*x+ %e *x+ %e", method, a,b,c,d)
set print
set term post
set output "./outfiles/" . method . ".ps"
plot f(x), "./outfiles/" . method . ".w.plot.txt"

method = "ddot"
set title "Messung und Approx: " . method
fit f(x) "outfiles/" . method . ".w.plot.txt" using 1:2 via a,b,c,d
str=fnmeth(0)
ddot(x)=@str
set print method . ".h"
print sprintf("#define %s_model(x) %e *x*x*x+ %e *x*x+ %e *x+ %e", method, a,b,c,d)
set print
set term post
set output "./outfiles/" . method . ".ps"
plot f(x), "./outfiles/" . method . ".w.plot.txt"

method = "daxpy"
set title "Messung und Approx: " . method
fit f(x) "outfiles/" . method . ".w.plot.txt" using 1:2 via a,b,c,d
str=fnmeth(0)
daxpy(x)=@str
set print method . ".h"
print sprintf("#define %s_model(x) %e *x*x*x+ %e *x*x+ %e *x+ %e", method, a,b,c,d)
set print
set term post
set output "./outfiles/" . method . ".ps"
plot f(x), "./outfiles/" . method . ".w.plot.txt"

method = "dgemv"
set title "Messung und Approx: " . method
fit f(x) "outfiles/" . method . ".w.plot.txt" using 1:2 via a,b,c,d
str=fnmeth(0)
dgemv(x)=@str
set print method . ".h"
print sprintf("#define %s_model(x) %e *x*x*x+ %e *x*x+ %e *x+ %e", method, a,b,c,d)
set print
set term post
set output "./outfiles/" . method . ".ps"
plot f(x), "./outfiles/" . method . ".w.plot.txt"

method = "dgemm"
set title "Messung und Approx: " . method
fit f(x) "outfiles/" . method . ".w.plot.txt" using 1:2 via a,b,c,d
str=fnmeth(0)
dgemm(x)=@str
set print method . ".h"
print sprintf("#define %s_model(x) %e *x*x*x+ %e *x*x+ %e *x+ %e", method, a,b,c,d)
set print
set term post
set output "./outfiles/" . method . ".ps"
plot f(x), "./outfiles/" . method . ".w.plot.txt"

set output "./outfiles/all_models.ps"
plot naiv(x), ddot(x), daxpy(x), dgemv(x), dgemm(x)

set output "./outfiles/all_measures.ps"
plotstring = "plot " . system("ls -m -Q ./outfiles/*.w.plot.txt")
@plotstring
EOF
